<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" confirmButtonText="导出"
            @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="编码开头" prop="prefix">
                    <el-input v-model="formData.prefix" placeholder="例如：NO."></el-input>
                </el-form-item>
                <el-form-item label="编码" prop="num">
                    <div style="display: flex">
                        <el-form-item>
                            <el-input-number v-model="formData.beginNum" placeholder="请输入编码"></el-input-number>
                        </el-form-item>
                        <span style="margin: 0 20px">至</span>
                        <el-form-item>
                            <el-input-number v-model="formData.endNum" placeholder="请输入编码"></el-input-number>
                        </el-form-item>
                    </div>
                </el-form-item>
                <el-form-item label="数量" prop="num">
                    <el-input v-model="formData.num" disabled></el-input>
                </el-form-item>
            </el-form>
            <div class="btn-group">
                <span>编码样式：{{ formData.prefix
                    }}{{ formData.beginNum.toString().padStart(5, '0') }}至{{
                        formData.prefix
                    }}{{ formData.endNum.toString().padStart(5, '0') }}</span>
                <span>数量：{{ codeNum }}</span>
            </div>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { computed, reactive, ref, shallowRef, watch } from 'vue'
import feedback from '@/utils/feedback'
import { exportApi } from '@/api/activity/codeBatch'
import { generaterApi } from '@/api/activity/code'

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = ref('导出码号')

const formData = reactive({
    num: 0,
    prefix: 'NO.',
    beginNum: 1,
    endNum: 10,
})

const formRules = reactive({
    num: [
        {
            required: true,
            message: '请输入数量',
            trigger: ['blur'],
        },
    ],
})
const codeNum = computed(() => {
    return formData.endNum - formData.beginNum + 1
})
watch(
    codeNum,
    () => {
        formData.num = codeNum.value
    },
    { immediate: true },
)
const handleSubmit = async () => {
    if (formData.num === 0) {
        feedback.msgError('数量不能为0')
        return
    }
    feedback.loading('正在导出中...')
    try {
        await exportApi(formData)
        feedback.closeLoading()
    } catch (error) {
        feedback.closeLoading()
    }
}
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
})
</script>
